<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>Pyscript Demo</title>
    <link rel="stylesheet" href="https://pyscript.net/alpha/pyscript.css" />
    <script defer src="https://pyscript.net/alpha/pyscript.js"></script>
    <!-- <script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-2.4.2.js"></script>
    <script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-widgets-2.4.2.min.js"></script>
    <script type="text/javascript" src="https://cdn.bokeh.org/bokeh/release/bokeh-tables-2.4.2.min.js"></script>
    <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@holoviz/panel@0.13.1/dist/panel.min.js"></script>
 -->
    <py-env>
        - pandas
        - scikit-learn
        - matplotlib
    </py-env>
</head>
<body>
<input  style="display:none;"  type="text" id="area" th:value="${teaShopOrder}"/>

<div id="plot" ></div>
</body>
</html>
<py-script output = "plot">
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from sklearn.linear_model import LinearRegression
import datetime
import random
from sklearn.cluster import Birch
from sklearn.cluster import KMeans

data = str(document.getElementById("area").value);
prices_dates = []
for item in data.split(","):
    price = int(item.split("price=")[1].split(" ")[0])
    date_str = item.split("date=")[1].strip("]").strip()
    date = datetime.datetime.strptime(date_str, "%Y-%m-%d")
    prices_dates.append([price, date])
x = [item[1].date() for item in prices_dates]
y = [item[0] for item in prices_dates]

z = {'Date':np.array(x), 'Money':np.array(y)}
df = pd.DataFrame(z)
df.set_index('Date', inplace=True)
df['Time'] = np.arange(len(x))
X = np.array(df)
clf = KMeans(n_clusters = 3, algorithm = 'elkan')
y_pred = clf.fit_predict(X)

cluster_centers = np.sort(clf.cluster_centers_, axis=0)

category = [0, 9999999, 0, 9999999, 0, 9999999]
for i in range(len(y_pred)):
    if (y_pred[i] == 2):
        category[0] = max(y[i], category[0])
        category[1] = min(y[i], category[1])
    if (y_pred[i] == 1):
        category[2] = max(y[i], category[2])
        category[3] = min(y[i], category[3])
    if (y_pred[i] == 0):
        category[4] = max(y[i], category[4])
        category[5] = min(y[i], category[5])
category = sorted(category, reverse = True)


plt.figure(figsize = (12, 6), dpi = 1000)
plt.grid(color='lightgray', linewidth=1, alpha=0.8)
labels = clf.labels_
cluster1 = X[labels == 0]
cluster2 = X[labels == 1]
cluster3 = X[labels == 2]
if max(cluster1[:, 0]) < max(cluster2[:, 0]):
    temp = cluster1
    cluster1 = cluster2
    cluster2 = temp
if max(cluster2[:, 0]) < max(cluster3[:, 0]):
    temp = cluster2
    cluster2 = cluster3
    cluster3 = temp
if max(cluster1[:, 0]) < max(cluster2[:, 0]):
    temp = cluster1
    cluster1 = cluster2
    cluster2 = temp
x = np.array(x)
date1 = [x[i] for i in cluster1[:, 1]]
date2 = [x[i] for i in cluster2[:, 1]]
date3 = [x[i] for i in cluster3[:, 1]]
plt.scatter(date1, cluster1[:, 0], c='tomato', label = 'High Consumption')
plt.scatter(date2, cluster2[:, 0], c='skyblue', label = 'Medium Consumption')
plt.scatter(date3, cluster3[:, 0], c='mediumseagreen', label = 'Low Consumption')
plt.axhline(y = (min(cluster1[:, 0]) + max(cluster2[:, 0])) / 2,color = 'slategray')
plt.axhline(y = (min(cluster2[:, 0]) + max(cluster3[:, 0])) / 2,color = 'slategray')
plt.title("K-means")
plt.xlabel("Date")
plt.ylabel("Consumption")
plt.legend()
plt

</py-script>
